package main

// 场景: WithValue携带数据(比如trace_id), 超时控制, 取消控制
import (
	"context"
	"fmt"
	"sync"
	"time"
)

var wg sync.WaitGroup

func worker(ctx context.Context) {
LOOP:
	for {
		fmt.Println("worker")
		time.Sleep(time.Second)

		select {
		case <-ctx.Done():
			break LOOP
		default:
		}
	}
	wg.Done()
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())

	wg.Add(1)
	go worker(ctx)

	time.Sleep(time.Second * 3)
	cancel()

	wg.Wait()
	fmt.Println("done")
}
